package Messages;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import ocsf.server.ConnectionToClient;
import server.SQLConnection;
import org.apache.commons.math3.stat.Frequency;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
public class MessageActivityMonth implements Runnable {

	private ConnectionToClient client;
	



	
	private ArrayList<Integer> numofactive= new ArrayList<Integer>();
	private double activeavg;
	private double activemid;
	private Frequency activefreq;
	
	private ArrayList<Integer> numoflock= new ArrayList<Integer>();
	private double lockavg;
	private double lockmid;
	private Frequency lockfreq;
	
	private ArrayList<Integer> numoffreeze= new ArrayList<Integer>();
	private double freezeavg;
	private double freezekmid;
	private Frequency freezefreq;
	
	private ArrayList<Integer> numoflend= new ArrayList<Integer>();
	private double lendavg;
	private double lendmid;
	private Frequency lendfreq;
	
	private ArrayList<Integer> numoflate= new ArrayList<Integer>();
	private double lateavg;
	private double latemid;
	private Frequency latefreq;

	private int row1;
	private static final int SEC = 1000;
	private static final int MIN = SEC * 60;
	private static final int HOUR = MIN * 60;
	private static final int DAY = HOUR * 24;
	private static final long WEEK = DAY * 7;
	private static final long MONTH = WEEK * 4;
	private static final long YEAR = WEEK * 52;
	private int flag=0 ; 
	/**
	 * constractor of this class
	 */
	
	public MessageActivityMonth() {
		super();
		this.client = client;
	}
/**
 * run the the the activity repory once amonth and get the data from the db
 */
	public void run() {
		
		
		try {
			ActivityMonth();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	
		
	
		
		
	}

	

private void ActivityMonth() throws SQLException {
	
	SQLConnection sc = new SQLConnection();
	PreparedStatement active = sc.getConnection().prepareStatement("SELECT active FROM db1.activity;");
	PreparedStatement lock = sc.getConnection().prepareStatement("SELECT locked FROM db1.activity;");
	PreparedStatement freeze = sc.getConnection().prepareStatement("SELECT freeze FROM db1.activity;");
	PreparedStatement lend = sc.getConnection().prepareStatement("SELECT lend FROM db1.activity;");
	PreparedStatement notintime = sc.getConnection().prepareStatement("SELECT notintime FROM db1.activity;");
	PreparedStatement del = sc.getConnection().prepareStatement("DELETE * FROM db1.activity;");
	PreparedStatement resultac = sc.getConnection().prepareStatement("INSERT INTO db1.activemonth (idactivemonth,average,Midian,Diffrence) VALUES(?,?,?,?);");
	PreparedStatement delrs = sc.getConnection().prepareStatement("DELETE FROM db1.activity;");

	
	
	sc.getConnection().setAutoCommit(true);
		
	java.sql.ResultSet rsactive =  active.executeQuery();
	
	while(rsactive.next())
	{
		numofactive.add((rsactive.getInt(1)));
		
		rsactive.next();
	}
	activeavg=calcAvarage(numofactive);
	System.out.println(activeavg);
	activemid=calcMedian(numofactive);
	System.out.println(activemid);
	activefreq=calcFreq(numofactive);
	System.out.println(activefreq);
	
	
	java.sql.ResultSet rslock =  lock.executeQuery();

	while(rslock.next())
	{
		numoflock.add((rslock.getInt(1)));
		
		rslock.next();
	}
	lockavg=calcAvarage(numoflock);
	System.out.println(lockavg);
	lockmid=calcMedian(numoflock);
	System.out.println(activemid);
	lockfreq=calcFreq(numoflock);
	System.out.println(activefreq);
	
	java.sql.ResultSet rsfreeze =  lock.executeQuery();

	while(rsfreeze.next())
	{
		numoffreeze.add((rsfreeze.getInt(1)));
		
		rsfreeze.next();
	}
	freezeavg=calcAvarage(numoffreeze);
	System.out.println(freezeavg);
	freezekmid=calcMedian(numoffreeze);
	System.out.println(freezekmid);
	freezefreq=calcFreq(numoffreeze);
	System.out.println(freezefreq);

	java.sql.ResultSet rslend =  lend.executeQuery();

	while(rslend.next())
	{
		numoflend.add((rslend.getInt(1)));
		
		rslend.next();
	}
	lendavg=calcAvarage(numoflend);
	System.out.println(lendavg);
	lendmid=calcMedian(numoflend);
	System.out.println(lendmid);
	lendfreq=calcFreq(numoflend);
	System.out.println(lendfreq);
	
	java.sql.ResultSet rslate =  notintime.executeQuery();

	while(rslate.next())
	{
		numoflate.add((rslate.getInt(1)));
		
		rslate.next();
	}
	lateavg=calcAvarage(numoflate);
	System.out.println(lateavg);
	latemid=calcMedian(numoflate);
	System.out.println(latemid);
	latefreq=calcFreq(numoflate);
	System.out.println(latefreq);
	
	
	
	try 
	{
		Thread.sleep(WEEK);
		delrs.executeUpdate(); // delete activity table 
	} 
	catch (InterruptedException e) 
	{
		e.printStackTrace();
	}
	
	
	}
		


protected double calcAvarage(ArrayList<Integer> list){
	DescriptiveStatistics stat = new DescriptiveStatistics();
	for (Integer x : list){
		stat.addValue(x);
	}
	return stat.getMean();

}

protected double calcMedian(ArrayList<Integer> a){		
	Integer[] list = new Integer[a.size()];
    a.toArray(list);
    Arrays.sort(list);
    Collections.sort(a);
    if (a.size() >0){
    	if((a.size() % 2) == 0) 
    		return ( ( a.get(a.size()/2) + a.get(a.size()/2-1) ) / 2);         
    	return ( a.get(a.size()/2) );   
    }
    return 0;
}

protected Frequency calcFreq(ArrayList<Integer> a){
	Frequency f = new Frequency();
	for (Integer x : a){
		f.addValue(x);
	}
	return f;
}

}
